/**
 * build_project
 * <p>
 * Copyright 2014 cayden.cn, Inc. All rights reserved.
 *
 * @author Administrator
 * @date 2021-06-23 10:42
 */
package com.cayden.config.boot;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

/**
 *
 * @author cayden
 * @date 2021-06-23 10:42
 */
public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext CONTEXT;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (CONTEXT != null) {
            if (applicationContext.getParent() == applicationContext) {
                setContext(applicationContext);
            }
        } else {
            setContext(applicationContext);
        }
    }

    public static ConfigurableApplicationContext get() {
        return (ConfigurableApplicationContext) CONTEXT;
    }

    private static void setContext(ApplicationContext applicationContext){
        CONTEXT = applicationContext;
    }

    public static ApplicationContext getApplicationContext(){
        return CONTEXT;
    }
}
